package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.whmskj.xjlhsc.entity.ACThatDay;
import com.whmskj.xjlhsc.mapper.ACThatDayMapper;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.dto.ACThatDay5Dto;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.dto.DeviceMinTimeDto;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.ACThatDayVo;
import com.whmskj.xjlhsc.mobie.equipmentManage.util.TimeFormatConverter;
import com.whmskj.xjlhsc.service.ACThatDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 张
 * @version 1.0
 */

@Service
public class ACThatDayServiceImpl extends ServiceImpl<ACThatDayMapper, ACThatDay> implements ACThatDayService {
    @Autowired
    private ACThatDayMapper acThatDayMapper;

    /**
     * 温湿度趋势
     *
     * @param mesId
     * @param machinenumber
     * @param day
     * @param startTime
     * @param endTime
     * @return
     */
//    @Override
//    public Map<String, List<ACThatDay>> actrend(List<Integer> mesId, List<String> machinenumber, String day, String startTime, String endTime) {
//
////        List<ACThatDay> list=new ArrayList<>();
////        if (isValidYmdFormat("yyyy-MM-dd HH:mm:ss",startTime)){
////            //每小时
////           list  = acThatDayMapper.selectACThatDay(mesId,machinenumber,startTime,endTime);
////        }
////        if (isValidYmdFormat("yyyy-MM-dd",startTime)){
////
////            list = acThatDayMapper.selectACThatDay2(mesId,machinenumber,startTime,endTime);
////        }
////        if (isValidYmdFormat("yyyy-MM",startTime)){
////            list = acThatDayMapper.selectACThatDay3(mesId,machinenumber,startTime,endTime);
////        }
////        Map<String, List<ACThatDay>> collect = list.stream().collect(Collectors.groupingBy(ACThatDay::getMachinenumber));
////        return collect;
//
//
//    }
    public Map<String, List<ACThatDayVo>> actrend(List<Integer> mesId, List<String> machinenumber, String day, String startTime, String endTime) {
        List<ACThatDayVo> list = acThatDayMapper.selectACThatDay4(machinenumber);
        Map<String, List<ACThatDayVo>> resultMap = new HashMap<>();
        
        if (CollectionUtil.isEmpty(list)){
            list = new ArrayList<>();
        }
        
        Map<String, List<ACThatDayVo>> acGruopListByName = list.stream().collect(Collectors.groupingBy(ACThatDayVo::getMachinenumber));
        
        for (String deviceName : machinenumber) {
            List<ACThatDayVo> acThatDayVoList = acGruopListByName.get(deviceName);
            if (CollectionUtil.isEmpty(acThatDayVoList)) {
                acThatDayVoList = new ArrayList<>();
            }
            
            List<ACThatDayVo> deviceDataList = new ArrayList<>();
            Map<Integer, List<ACThatDayVo>> hourMap = acThatDayVoList.stream().collect(Collectors.groupingBy(ACThatDayVo::getHour));
            
            for (int i = 0; i < 24; i++) {
                List<ACThatDayVo> acThatDayVos = hourMap.get(i);
                ACThatDayVo acThatDayVo = new ACThatDayVo();
                if (acThatDayVos == null) {
                    acThatDayVo.setMachinenumber(deviceName);
                    acThatDayVo.setHour(i);
                    acThatDayVo.setSj001(0.0);
                    acThatDayVo.setSj003(0.0);
                } else {
                    acThatDayVo = acThatDayVos.get(0);
                }
                deviceDataList.add(acThatDayVo);
            }
            resultMap.put(deviceName, deviceDataList);
        }
        
        return resultMap;
    }

    /**
     * 验证时间字符串是否符合 yyyy-MM-dd 格式
     *
     * @param dateStr 待验证的时间字符串
     * @return 符合格式返回true，否则返回false
     */
    public static boolean isValidYmdFormat(String pattern, String dateStr) {
        // 校验入参是否为空
        if (pattern == null || pattern.trim().isEmpty() ||
                dateStr == null || dateStr.trim().isEmpty()) {
            return false;
        }

        SimpleDateFormat sdf = null;
        String trimmedDate = dateStr.trim();

        try {
            // 初始化格式化器并设置严格模式
            sdf = new SimpleDateFormat(pattern, Locale.US);
            sdf.setLenient(false); // 禁用宽松解析（如拒绝2月30日、13月等）

            // 尝试解析字符串（解析失败直接抛异常）
            sdf.parse(trimmedDate);

            // 关键校验：将解析后的日期重新格式化，与原始字符串对比
            // 确保格式完全匹配（包括位数，如月份必须是两位）
            String formatted = sdf.format(sdf.parse(trimmedDate));
            return formatted.equals(trimmedDate);

        } catch (IllegalArgumentException e) {
            // 传入的pattern格式非法（如包含无效占位符）
            return false;
        } catch (ParseException e) {
            // 时间字符串不符合pattern格式（如位数不对、分隔符错误等）
            return false;
        }
    }

}
